/**
 * 通常给魔法书选择一个内容时使用
 * 释放了魔法书里的技能 然后再从施法者删除此魔法书
 */
import BaseUtil from "../../solar/util/BaseUtil";

export default class DeleteAbilityFromUnitOnUseAbilityState {

    //key为要删除的技能 数组值为当使用这些技能时触发删除
    static config: { [id: string]: string[] } = {}

    private static cache: { [id: string]: string } = {}

    constructor() {
        //init cache
        let config = DeleteAbilityFromUnitOnUseAbilityState.config;
        for (let configKey in config) {
            let configData: string[] = config[configKey];
            for (let configDatum of configData) {
                DeleteAbilityFromUnitOnUseAbilityState.cache[configDatum] = configKey;
            }
        }
        //init
        let triggerHandle = CreateTrigger();
        TriggerRegisterAnyUnitEventBJ(triggerHandle, EVENT_PLAYER_UNIT_SPELL_EFFECT);
        TriggerAddAction(triggerHandle, this.action);
    }

    action(this: void) {
        let spellAbilityIdStr = id2string(GetSpellAbilityId());
        let needDeleteAbilityIdStr = DeleteAbilityFromUnitOnUseAbilityState.cache[spellAbilityIdStr];
        if (needDeleteAbilityIdStr) {
            let unit = GetTriggerUnit();
            //等待一下再删除 以免获取技能的触发获取不到释放技能id
            BaseUtil.runLater(0.01,()=>{
                UnitRemoveAbility(unit, FourCC(needDeleteAbilityIdStr));
            })
        }

    }


}

